package org.gdstash.ui;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.select.AffixSelectionPane;
import org.gdstash.ui.select.SelectAndOrPane;
import org.gdstash.util.GDImagePool;
import org.gdstash.util.GDLog;
import org.gdstash.util.GDMsgFormatter;
import org.gdstash.util.GDMsgLogger;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GDFilterDialog extends JDialog {
    private Component owner;
    private JButton btnSearch;
    private JButton btnClear;
    private SelectAndOrPane pnlAndOr;
    private AffixSelectionPane pnlSelect;
    private GDUIFilter uiFilter;
    private JPanel pnlMain;

    public GDFilterDialog(final GDUIFilter uiFilter) {
        this(null, uiFilter, true);
    }

    public GDFilterDialog(final Frame owner, final GDUIFilter uiFilter) {
        this(owner, uiFilter, true);
    }

    public GDFilterDialog(final Frame owner, final GDUIFilter uiFilter, final boolean modal) {
        super(owner, modal);
        this.owner = owner;
        this.uiFilter = uiFilter;
        this.adjustUI();
        this.pnlMain = this.buildMainPanel();
        this.getContentPane().add(this.pnlMain);
        this.pack();
        this.setLocationRelativeTo(owner);
        this.getRootPane().registerKeyboardAction(new CloseActionListener(), KeyStroke.getKeyStroke(27, 0), 2);
        this.getRootPane().setDefaultButton(this.btnSearch);
        this.btnSearch.requestFocus();
    }

    public void adjustUI() {
        this.setTitle(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_FILTER"));
        final Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntButton = UIManager.getDefaults().getFont("Button.font");
        if (fntButton == null) {
            fntButton = fntLabel;
        }
        fntButton = fntButton.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        if (this.btnSearch == null) {
            (this.btnSearch = new JButton()).addActionListener(new FilterActionListener());
        }
        this.btnSearch.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_FILTER"));
        this.btnSearch.setIcon(GDImagePool.iconBtnFilter24);
        this.btnSearch.setFont(fntButton);
        GDStashFrame.setMnemonic(this.btnSearch, "MNC_FILTER");
        if (this.btnClear == null) {
            (this.btnClear = new JButton()).addActionListener(new ClearActionListener());
        }
        this.btnClear.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_CLEAR"));
        this.btnClear.setIcon(GDImagePool.iconItemDelete24);
        this.btnClear.setFont(fntButton);
        GDStashFrame.setMnemonic(this.btnClear, "MNC_CLEAR");
        if (this.pnlAndOr == null) {
            this.pnlAndOr = new SelectAndOrPane();
        } else {
            this.pnlAndOr.adjustUI();
        }
        if (this.pnlSelect == null) {
            this.pnlSelect = new AffixSelectionPane();
        } else {
            this.pnlSelect.adjustUI();
        }
        this.pack();
    }

    private JPanel buildMainPanel() {
        final JPanel panel = new JPanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final JPanel pnlButton = this.buildButtonPanel();
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(pnlButton).addComponent(this.pnlSelect));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(pnlButton)).addGroup(layout.createParallelGroup().addComponent(this.pnlSelect));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, pnlButton, this.pnlSelect);
        return panel;
    }

    private JPanel buildButtonPanel() {
        final BorderLayout layout = new BorderLayout();
        final JPanel panel = new JPanel();
        panel.setLayout(layout);
        panel.add(this.btnSearch, "Center");
        panel.add(this.btnClear, "West");
        panel.add(this.pnlAndOr, "East");
        return panel;
    }

    private class CloseActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            GDFilterDialog.this.setVisible(false);
        }
    }

    private class FilterActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            final GDLog log = new GDLog();
            final SelectionCriteria criteria = new SelectionCriteria();
            GDFilterDialog.this.pnlAndOr.addCriteria(criteria);
            GDFilterDialog.this.pnlSelect.addCriteria(criteria);
            criteria.checkCriteria(log);
            if (log.containsErrors()) {
                GDMsgLogger.addLog(log);
                GDMsgLogger.showLog(GDFilterDialog.this, GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_FILTER"));
                return;
            }
            GDFilterDialog.this.setCursor(Cursor.getPredefinedCursor(3));
            if (GDFilterDialog.this.uiFilter != null) {
                GDFilterDialog.this.uiFilter.filter(criteria);
            }
            GDFilterDialog.this.setVisible(false);
            GDFilterDialog.this.setCursor(Cursor.getDefaultCursor());
        }
    }

    private class ClearActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            GDFilterDialog.this.pnlAndOr.clear();
            GDFilterDialog.this.pnlSelect.clear();
        }
    }
}
